/*
* @Author: ylq
 * @Date: 2021-08-24 16:58:33
 * @Desc: 工具箱
 * @Last Modified by: ylq
 * @Last Modified time: 2022-08-15 11:40:24
 */
import Vue from 'vue'

class Tools {
  constructor() {
    this.eventBus = new Vue()
  }
  lazy(comp) {
    return () => {
      return new Promise(resolve => {
        let isAfterDelay = false
        const timer = setTimeout(() => {
          isAfterDelay = true
          this.eventBus.$emit('loadingChange', true)
        }, 300)
        comp()
          .then(res => {
            if (isAfterDelay) {
              this.eventBus.$emit('loadingChange', false)
            } else {
              clearTimeout(timer)
            }
            resolve(res)
          })
          .catch(err => {
            console.error(err)
            location.reload()
          })
      })
    }
  }
  // 设置cookie
  setCookie(name, value, t) {
    var days = (t === undefined ? 1 : t) * 24 * 60 * 60 * 1000;
    var exp = new Date();
    exp.setTime(exp.getTime() + days);
    document.cookie = name + "=" + escape(value) + ";path=/;expires=" + exp.toGMTString();
  }
  // 获取cookie
  getCookie(key) {
    var strCookie = document.cookie;
    var arrCookie = strCookie.split(";");
    for (var m = 0; m < arrCookie.length; m++) {
      var ac = arrCookie[m].replace(/\s*/g, '');
      if (ac.indexOf(key + '=') != -1) {
        return ac.replace(key + '=', "");
      }
    }
  }


  //日期转换 返回为:2018-09-05  0为当前日期
  getyyyyMMdd(a) {
    var d;
    if (a == 0) {
      d = new Date();
    } else {
      (typeof a == 'string') && (a = a.replace(/-/g,'/'))
      d = new Date(a);
    }
    var curr_date = d.getDate();
    var curr_month = d.getMonth() + 1;
    var curr_year = d.getFullYear();
    String(curr_month).length < 2 ? (curr_month = "0" + curr_month) : curr_month;
    String(curr_date).length < 2 ? (curr_date = "0" + curr_date) : curr_date;
    var yyyyMMdd = curr_year + "-" + curr_month + "-" + curr_date;
    return yyyyMMdd;
  }
  //日期含时分秒
  getDateHasTime(a) {
    var d;
    if (!a) {
      d = new Date();
    } else {
      (typeof a == 'string') && (a = a.replace(/-/g,'/'))
      d = new Date(a);
    }
    var dt = d.getDate();
    var month = d.getMonth() + 1;
    var yy = d.getFullYear();
    var hh = d.getHours();
    var mm = d.getMinutes();
    var ss = d.getSeconds();
    String(month).length < 2 ? (month = "0" + month) : month;
    String(dt).length < 2 ? (dt = "0" + dt) : dt;
    String(hh).length < 2 ? (hh = "0" + hh) : hh;
    String(mm).length < 2 ? (mm = "0" + mm) : mm;
    String(ss).length < 2 ? (ss = "0" + ss) : ss;
    var t = yy + "-" + month + "-" + dt + ' ' + hh + ':' + mm + ':' + ss;
    return t;
  }

  
}

export const tools = new Tools()